/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica;


import GUI.pdf;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
/**
 *
 * @author andresgodoysalavert
 */
public class FabricaWebService {

    protected String tipo;


    public void enviarToken(String ip){

        if(tipo.equals("soap")){


        }else if(tipo.equals("rest")){
            try {

                URL url = new URL("http://"+ip+":3000/web_service/token");
                System.out.println("URL: "+url);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "text/xml");
                OutputStream os = connection.getOutputStream();
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer transformer = tf.newTransformer();
                FileReader fileReader = new FileReader("peticion.xml");
                StreamSource source = new StreamSource(fileReader);
                StreamResult result = new StreamResult(os);
                transformer.transform(source, result);
                os.flush();
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine = in.readLine();
                String token = "";
                if (inputLine.equals("<token>")){
                    inputLine = in.readLine();
                    StringTokenizer tokens=new StringTokenizer(inputLine,"<id_token>/");
                    token = tokens.nextToken();
                    //token = tokens.nextToken();
                    System.out.println(token);
                    escribirXML(token);
                    enviarXML(ip);
                }else if(inputLine.equals("<error>")){
                    JOptionPane.showMessageDialog(null,
                    "Maquina Inhabilitada!",
                    "Error en Transaccion",
                    JOptionPane.WARNING_MESSAGE);

                    // USA ESTE WARNING PARA EL CACTH DE IRENE SI NO SE CONECTO!!!!!!!
                }
                in.close();
                connection.getResponseCode();
                connection.disconnect();
            } catch (TransformerException ex) {
                Logger.getLogger(FabricaXML.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FabricaXML.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
    }


    public void enviarXML(String ip){

        if(tipo.equals("soap")){
           // try{
            //CODIGO PA ENVIAR A IRENE
          //  } catch (IOException ie){

          //  }

        }else if(tipo.equals("rest")){


            try {
                URL url = new URL("http://"+ip+":3000/web_service/envio_apuesta");
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setInstanceFollowRedirects(false);
                connection.setRequestMethod("POST");
                connection.setRequestProperty("Content-Type", "text/xml");
                OutputStream os = connection.getOutputStream();
                TransformerFactory tf = TransformerFactory.newInstance();
                Transformer transformer = tf.newTransformer();
                FileReader fileReader = new FileReader("Apuestasweb.xml");
                StreamSource source = new StreamSource(fileReader);
                StreamResult result = new StreamResult(os);
                transformer.transform(source, result);
                os.flush();
                BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String inputLine = "";
                String evidencia = "";
                while ((inputLine = in.readLine()) != null){
                    evidencia = evidencia + inputLine + "\n";
                }
                pdf recibo = new pdf(evidencia);
                in.close();
                connection.getResponseCode();
                connection.disconnect();
            } catch (TransformerException ex) {
                Logger.getLogger(FabricaXML.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(FabricaXML.class.getName()).log(Level.SEVERE, null, ex);
                //ENVIO A IRENEEEE!!!!!!!!!!!!!
                //FabricaWebService a = new FabricaWebService("soap");
                //a.enviarToken();

            }

        }
    }

    public void escribirXML(String token){
         if(tipo.equals("soap")){
           // try{
            //CODIGO PA ENVIAR A IRENE
          //  } catch (IOException ie){

          //  }

        }else if(tipo.equals("rest")){
            EscribirApuestaXmlWEB lista = new EscribirApuestaXmlWEB();
            lista.escribirApuestas(token);
         }
    }


    public FabricaWebService(String t){
        tipo = t;
    }


}
